﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZICModel;
using ZICModel.Fault;

namespace ZICModel.Common
{
    public class ZICBaseModel
    {
        protected static ZIS2Entities zic2Entities = new ZIS2Entities();

        public static List<string> TroubleExplain = new List<string>(); //version,code,explain
        protected int faultCount = 0;

        public ZICBaseModel()
        {
            zic2Entities.tbExCodes.ToList().ForEach(t =>
                {
                    TroubleExplain.Add(string.Format("[{0}][{1}][{2}]", t.isEx.ToString().PadLeft(2,'0'), t.code.Trim(), t.descn.Trim().Replace(" ",string.Empty)));
                });
        }

        protected void SaveFault(FaultBase fault)
        {
            foreach (MainFaultDetail mainFault in fault.FaultBody.ZISMainFaultList)
            {
                foreach (SubFaultDetail subFault in mainFault.ZISSubFaultList)
                {
                    WiseLinkFault wiseLinkFault = new WiseLinkFault();
                    wiseLinkFault.Code = subFault.ConvertedCode;
                    wiseLinkFault.FaultType = (short)FaultType.Fault;
                    wiseLinkFault.GPSDate = fault.FaultHeader.GPSTime;
                    wiseLinkFault.IDCSimNumber = fault.FaultHeader.Idc;
                    wiseLinkFault.Mileage = fault.FaultHeader.Mileage;
                    wiseLinkFault.TroubleExplain = subFault.TroubleExplain;
                    wiseLinkFault.SrcFaultID = fault.FaultHeader.SrcFaultId;
                    wiseLinkFault.ReceiveDate = DateTime.Now;
                    wiseLinkFault.CodeVersion = fault.FaultHeader.Version;

                    lock (this)
                    {
                        zic2Entities.WiseLinkFaults.AddObject(wiseLinkFault);
                    }

                }
            }
            
            
        }


        protected void SaveUrgent(Urgent urgent)
        {

            WiseLinkFault wiseLinkFault = new WiseLinkFault();
            wiseLinkFault.FaultType = (short)FaultType.Urgent;
            wiseLinkFault.GPSDate = urgent.GPSTime;
            wiseLinkFault.IDCSimNumber = urgent.Idc;
            wiseLinkFault.SrcFaultID = urgent.SrcFaultId;
            wiseLinkFault.ReceiveDate = DateTime.Now;
            wiseLinkFault.Latitude = urgent.Latitude.ToString();
            wiseLinkFault.Longitude = urgent.Longitude.ToString();
            lock (this)
            {
                zic2Entities.WiseLinkFaults.AddObject(wiseLinkFault);
            }
            


        }
    }
}
